using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Dragoon.BreakIt
{
    public enum PaddleSize {Short, Normal, Long};
    public class Paddle : Movable
    {

        static Vector3 GetDimension(PaddleSize size,int paddleHeight)
        {
            const float WIDTH_FRACTION = 0.15F;
            float widthFactor = 1;
            switch (size)
            {
                case PaddleSize.Long:
                    widthFactor = 1.5F;
                    break;
                case PaddleSize.Short:
                    widthFactor = 0.5F;
                    break;
            }
            return 
                new Vector3(widthFactor*WIDTH_FRACTION*Geometry.MaxX,
                    paddleHeight,1);
        }

        static Vector3 GetOrigen(Rectangle playArea, int paddleHeight)
        {
            return new Vector3(
                playArea.Left + playArea.Width/2.0F, 
                playArea.Bottom - (paddleHeight/2),0);
        }

        static float Speed(Rectangle playArea)
        {
            return playArea.Width * 0.5F;
        }

        readonly Rectangle _playArea;
        readonly int _height;
        private int _leftBoundary, _rightBoundary;
        private PaddleSize _size;

        internal Paddle(Rectangle playArea, int paddleHeight)
            : base(GetDimension(PaddleSize.Normal,paddleHeight), 
                   GetOrigen(playArea, paddleHeight), Speed(playArea))
        {
            _playArea = playArea;
            _size = PaddleSize.Normal;
            _height = paddleHeight;
            ResetLimits();
        }

        internal PaddleSize Size
        {
            get 
            {
                return _size;
            }
            set
            {
                if (_size == value)
                    return;
                _size = value;
                SetDimensions(GetDimension(_size, _height));
                ResetLimits();
            }
        }

        internal void ResetLimits()
        {
            int paddleMiddle = Position.Width / 2;
            _leftBoundary = _playArea.Left + paddleMiddle;
            _rightBoundary = _playArea.Right - paddleMiddle;
        }

        private void SetPaddleLimits()
        {
            int paddleMiddle = Position.Width / 2;
            _leftBoundary = Position.Left;
            _rightBoundary = Position.Right;
        }

        static readonly Rectangle SRC_NORMAL = new Rectangle(235, 175, 44, 12);
        public Rectangle ImageSourceArea
        {
            get
            {
                return SRC_NORMAL;
            }
        }

        public void MoveLeft(GameTime time)
        {
            StartMoving(time, new Vector3(-1,0,0));
        }

        public void MoveRight(GameTime time)
        {
            StartMoving(time, new Vector3(1, 0, 0));
        }

        public void Stop(GameTime time)
        {
            StopMoving(time);
        }

        protected override Vector3 LocationAt(GameTime time)
        {
            Vector3 r = base.LocationAt(time);
            if (r.X < _leftBoundary)
                r.X = _leftBoundary;
            else if (r.X > _rightBoundary)
                r.X = _rightBoundary;
            return r;
        }

        internal void Intialise()
        {
            Size = PaddleSize.Normal;
            SetOrigin(GetOrigen(_playArea,Position.Height));
            SetPaddleLimits();                      
        }
    }
}
